import mongoose from 'mongoose';
import { formatDateByNow } from '~/utils/util.date';

export interface ISubCategory extends mongoose.Document {
	name: string;
	description: string;
	parentId: mongoose.Types.ObjectId;
	createdAt?: string;
	updatedAt?: string;
}

// 二级分类（子分类）
const SubCategorySchema = new mongoose.Schema<ISubCategory>(
	{
		name: {
			type: String,
			required: true,
			unique: true,
		},
		description: {
			type: String,
			required: true,
		},
		parentId: {
			type: mongoose.Schema.Types.ObjectId,
			ref: 'MainCategory',
		},
		createdAt: {
			type: String,
			default: formatDateByNow(),
		},
		updatedAt: {
			type: String,
			default: formatDateByNow(),
		},
	},
	{
		collection: 'sub_categories',
	}
);

export const SubCategoryModel = mongoose.model('SubCategory', SubCategorySchema);
